package org.example.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.example.bean.Banner;
import org.example.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/banner")
@AllArgsConstructor
@Tag(name = "轮播图管理",description = "提供对轮播图的增删改查操作")
public class BannerController {
    public static final int BANNER_STATUS_ACTIVE= 1;
    public static final int BANNER_STATUS_INACTIVE= 0;
    @Autowired
    private final BannerService bannerService;

    @RequestMapping(value = "/add" , method = RequestMethod.POST)
    @Operation(summary = "添加轮播图",description = "添加轮播图信息")
    public ResponseEntity<ResponseResult> addBanner(@RequestParam("imageFile") MultipartFile imageFile,
                                    @RequestParam("link_url") String link_url,
                                    @RequestParam("title") String title){
        Banner banner = new Banner();
        banner.setBanner_status(BANNER_STATUS_ACTIVE);
        banner.setLink_url(link_url);
        banner.setImageFile(imageFile);
        banner.setTitle(title);

        bannerService.addBanner(banner);

        banner.setImageFile(null);  // 上传到OSS后，将文件流关闭，否则会导致内存泄漏

        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("添加成功").data(banner).build());
    }

    @GetMapping("/getBannersByStatus/{bannerStatus}")
    @Operation(summary = "根据状态查询轮播图",description = "根据状态查询轮播图信息")
    public ResponseEntity<ResponseResult> getBannersByStatus(@PathVariable("bannerStatus") Integer bannerStatus){
        List<Banner> banners = bannerService.getBannersByStatus(bannerStatus);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("查询成功").data(banners).build());
    }

    @PutMapping("/updateBannerStatus/{bannerId}/{bannerStatus}")
    @Operation(summary = "根据id修改轮播图的状态信息",description = "根据id修改轮播图的状态信息")
    public ResponseEntity<ResponseResult> updateBannerStatus(@PathVariable("bannerId") Integer bannerId,
                                                             @PathVariable("bannerStatus") Integer bannerStatus){
        boolean result = bannerService.updateBannerStatus(bannerId, bannerStatus);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("修改成功").data(result).build());
    }
}
